package com.gxa.shop.servlet.brand;

import com.gxa.shop.bean.pojo.Brand;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.brand.BrandService;
import com.gxa.shop.service.brand.impl.BrandServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * com.gxa.shop.bean.servlet.brand
 * User: zjy
 * Date: 2024/12/18 10:59
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
@WebServlet(name = "BrandAddServlet",value = "/brand/add")
public class BrandAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        BufferedReader bufferedReader=req.getReader();
        String  req_data=bufferedReader.readLine();
        Brand brand= JSONObject.parseObject(req_data,Brand.class);
        BrandService brandService=new BrandServiceImpl();

        boolean update = brandService.insert(brand);
        R r=!update? R.error("添加失败"):R.ok();
        //
        PrintWriter writer = resp.getWriter();
        String  result= JSON.toJSONString(r);
        System.out.println("AddServlet:"+result);
        writer.write(result);
        writer.flush();
        writer.close();
    }
}